//
//  CAPAFNetworkingTools.swift
//  CAPSwift
//
//  Created by captain on 16/4/19.
//  Copyright © 2016年 captain. All rights reserved.
//

import UIKit
import AFNetworking

//枚举，GET，POST请求
enum CAPRequestMethod: Int{
    case GET = 0
    case POST = 1
}


class CAPAFNetworkingTools: AFHTTPSessionManager {

    // MARK: - 单例
    static let shardTools: CAPAFNetworkingTools = {
       
        let tools = CAPAFNetworkingTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    
    // MARK: - 网络请求
    func request(method: CAPRequestMethod, urlString: String, params: AnyObject?, callback:(respose: AnyObject?, error: NSError?) -> () ){
        
        if method == .GET {
            
            self.GET(urlString, parameters: params, success: { (_, response) -> Void in
                callback(respose: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(respose: nil, error: error)
            })
            
            
        }else{
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                callback(respose: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(respose: nil, error: error)
                    
            })
        }
        
        
    }
    
    
    
    
}
